package com.lbl.third.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lbl.third.common.R;
import com.lbl.third.enytity.User;
import com.lbl.third.service.UserService;
import com.lbl.third.utils.ValidateCodeUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private UserService userService;

    /**
     * 发送验证码
     * @param user
     * @param httpSession
     * @return
     */
    @PostMapping("/sendMsg")
    public R<String> sendMsg(@RequestBody User user, HttpSession httpSession){
        //获取手机号
        String phone=user.getPhone();

        if(StringUtils.isNotEmpty(phone)){
            //生成4位随机验证码
            String code= ValidateCodeUtils.generateValidateCode(4).toString();

            System.out.println(code);
            //调用阿里云提供的短信服务Api完成发送短信
            //SMSUtils.sendMessage("阿里云短信测试","SMS_154950909",phone,code);


            //需要将生成的验证码保存到Session
            //httpSession.setAttribute(phone,code);

            //使用redis进行验证码存储
            redisTemplate.opsForValue().set(phone,code,5, TimeUnit.MINUTES);


            return R.success("手机验证码发送成功");

        }

        return R.error("短信发送失败");

    }


    /**
     * 移动端用户登录
     * @param map
     * @param httpSession
     * @return
     */
    @PostMapping("/login")
    public R<User> login(@RequestBody Map map, HttpSession httpSession){

        //获取手机号
        String phone = map.get("phone").toString();

        //获取验证码
        String code = map.get("code").toString();

        //从Session中获取保存验证码
       //Object code0 = httpSession.getAttribute(phone);

        //从redis中获取验证码
        Object code0 = redisTemplate.opsForValue().get(phone);


        //进行验证码对比（页面提交的验证码和Session中保存的验证码对戏）
        if(code0!=null&&code0.equals(code)){

            //对比成功说明登陆成功
            LambdaQueryWrapper<User> queryWrapper=new LambdaQueryWrapper<>();
            queryWrapper.eq(User::getPhone,phone);

            User user=userService.getOne(queryWrapper);
            if(user==null){
                //判断当前手机号对应的用户是否为新用户，如果为新用户自动完成注册
                user=new User();
                user.setPhone(phone);
                user.setStatus(1);
                userService.save(user);
            }
            httpSession.setAttribute("user",user.getId());
            redisTemplate.delete(phone);
            return R.success(user);
        }

       return R.error("验证失败");
    }

    /**
     * 移动端退出功能
     * @param httpSession
     * @return
     */
    @PostMapping("/loginout")
    public R<String> logout(HttpSession httpSession){
        httpSession.removeAttribute("user");
        return R.success("退出成功");
    }

}
